package com.ruoyi.safe.controller.web;

import java.util.*;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.quality.domain.TQualityFileItem;
import com.ruoyi.quality.service.ITQualityFileItemService;
import com.ruoyi.safe.domain.BizProjectCheckRecord;
import com.ruoyi.safe.domain.dto.BizLogStatisticsDTO;
import com.ruoyi.safe.domain.dto.BizRecordIdsDTO;
import com.ruoyi.safe.domain.dto.BizSecurityLogDTO;
import com.ruoyi.safe.service.IBizProjectCheckRecordService;
import org.jetbrains.annotations.NotNull;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.safe.domain.BizSecurityLog;
import com.ruoyi.safe.service.IBizSecurityLogService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 安全日志Controller
 *
 * @author ruoyi
 * @date 2023-12-01
 */
@RestController
@RequestMapping("/security/log")
public class BizSecurityLogController extends BaseController
{
    @Autowired
    private IBizSecurityLogService bizSecurityLogService;
    @Autowired
    private IBizProjectCheckRecordService bizProjectCheckRecordService;
    @Autowired
    private ITQualityFileItemService itQualityFileItemService;

    /**
     * 查询安全日志列表
     */
    @PreAuthorize("@ss.hasPermi('security:log:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizSecurityLogDTO bizSecurityLogDTO)
    {
        startPage();
        BizSecurityLog bizSecurityLog = getBizSecurityLog(bizSecurityLogDTO);
        List<BizSecurityLog> list = bizSecurityLogService.selectBizSecurityLogList(bizSecurityLog);
        return getDataTable(list);
    }

    @NotNull
    private static BizSecurityLog getBizSecurityLog(BizSecurityLogDTO bizSecurityLogDTO) {
        BizSecurityLog bizSecurityLog = new BizSecurityLog();
        bizSecurityLog.setSercurityPeople(bizSecurityLogDTO.getSercurityPeople());
        bizSecurityLog.setProjectId(bizSecurityLogDTO.getProjectId());
        bizSecurityLog.setSecurityType(bizSecurityLogDTO.getSecurityType());

        if(StringUtils.isNotNull(bizSecurityLogDTO.getSecurityDate())){
            Map<String, Object> params = new HashMap<>();
            params.put("securityStartDate", bizSecurityLogDTO.getSecurityDate()[0]);
            params.put("securityEndDate", bizSecurityLogDTO.getSecurityDate()[1]);
            bizSecurityLog.setParams(params);
        }
        return bizSecurityLog;
    }

    /**
     * 查询日志视图--本月数据列表
     */
    @PreAuthorize("@ss.hasPermi('security:log:list')")
    @GetMapping("/logView")
    public TableDataInfo logView(BizSecurityLogDTO bizSecurityLogDTO)
    {
        startPage();
        BizSecurityLog bizSecurityLog = getBizSecurityLog(bizSecurityLogDTO);
        List<BizSecurityLog> list = bizSecurityLogService.selectLogList(bizSecurityLog,
                SecurityUtils.getLoginUser().getUser());
        return getDataTable(list);
    }

    /**
     * 导出安全日志列表
     */
    @PreAuthorize("@ss.hasPermi('security:log:export')")
    @Log(title = "安全日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizSecurityLog bizSecurityLog)
    {
        List<BizSecurityLog> list = bizSecurityLogService.selectBizSecurityLogList(bizSecurityLog);
        ExcelUtil<BizSecurityLog> util = new ExcelUtil<BizSecurityLog>(BizSecurityLog.class);
        util.exportExcel(response, list, "安全日志数据");
    }

    /**
     * 获取安全日志详细信息
     */
    @PreAuthorize("@ss.hasPermi('security:log:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        AjaxResult ajaxResult = new AjaxResult();
        List<TQualityFileItem> list = new ArrayList<>();
        BizSecurityLog bizSecurityLog = bizSecurityLogService.selectBizSecurityLogById(id);
        if(!CollectionUtils.isEmpty(bizSecurityLog.getRecordIds())){
            bizSecurityLog.getRecordIds().stream().forEach(item -> {
                TQualityFileItem tQualityFileItem = itQualityFileItemService.selectTQualityFileItemById(item);
                list.add(tQualityFileItem);
            });
        }
        ajaxResult.put("bizProjectCheckRecord", list);
        ajaxResult.put("bizSecurityLog", bizSecurityLog);
        return success(ajaxResult);
    }

    /**
     * 新增安全日志
     */
    @PreAuthorize("@ss.hasPermi('security:log:add')")
    @Log(title = "安全日志", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizSecurityLog bizSecurityLog)
    {
        return toAjax(bizSecurityLogService.insertBizSecurityLog(bizSecurityLog,
                SecurityUtils.getLoginUser().getUser()));
    }

    /**
     * 修改安全日志
     */
    @PreAuthorize("@ss.hasPermi('security:log:edit')")
    @Log(title = "安全日志", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizSecurityLog bizSecurityLog)
    {
        return toAjax(bizSecurityLogService.updateBizSecurityLog(bizSecurityLog,
                SecurityUtils.getLoginUser().getUser()));
    }

    /**
     * 删除安全日志
     */
    @PreAuthorize("@ss.hasPermi('security:log:remove')")
    @Log(title = "安全日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bizSecurityLogService.deleteBizSecurityLogByIds(ids));
    }

    /**
     * 查询未添加的检查记录
     */
    @GetMapping("/unRecordList")
    public TableDataInfo unRecordList(TQualityFileItem tQualityFileItem){
        startPage();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        tQualityFileItem.setCheckPeopleId(String.valueOf(loginUser.getUserId()));
        List<TQualityFileItem> list = itQualityFileItemService.selectTQualityFileItemList(tQualityFileItem);
        return getDataTable(list);
    }

    /**
     * 安全日志统计
     * @return
     */
    @GetMapping("/logStatistics")
    public TableDataInfo selectLogStatistics(BizLogStatisticsDTO bizLogStatisticsDTO){
        startPage();
        List<Map<String,Object>> list = bizSecurityLogService.selectLogStatistics(bizLogStatisticsDTO);
        return getDataTable(list);
    }

    /**
     * 查询项目下的安全日志
     * @param bizLogStatisticsDTO
     * @return
     */
    @GetMapping("/getListByProjectId")
    public TableDataInfo getListByProjectId(BizLogStatisticsDTO bizLogStatisticsDTO){
        startPage();
        List<BizSecurityLog> bizSecurityLogList = bizSecurityLogService.selectListByDto(bizLogStatisticsDTO);
        return getDataTable(bizSecurityLogList);
    }

    /**
     * 批量新增检查记录
     * @param bizRecordIdsDTO
     * @return
     */
    @PutMapping("/insertRecords")
    public AjaxResult insertRecords(@RequestBody BizRecordIdsDTO bizRecordIdsDTO){
        return toAjax(bizSecurityLogService.insertRecords(bizRecordIdsDTO));
    }

    /**
     * 查询已添加的数据
     * @param createTime
     * @return
     */
    @GetMapping("/allRecordList")
    public TableDataInfo allRecordList(@RequestParam String createTime){
        startPage();
        List<BizProjectCheckRecord> list = bizProjectCheckRecordService.selectAllRecordList(createTime);
        return getDataTable(list);
    }
}
